
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map.Entry;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.tompai.datagather.model.CrawlDatum;

/**
* @author: tompai
* @createTime: 2018年4月28日 下午7:42:41
* @history:
* @version: v1.0
*/
public class CrawlDatumFormater {

    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String datumToString(CrawlDatum datum) {
        StringBuilder sb = new StringBuilder();
        sb.append("\nKEY: ").append(datum.key())
                .append("\nURL: ").append(datum.url())
                .append("\nSTATUS: ");

        switch (datum.getStatus()) {
            case CrawlDatum.STATUS_DB_SUCCESS:
                sb.append("success");
                break;
            case CrawlDatum.STATUS_DB_FAILED:
                sb.append("failed");
                break;
            case CrawlDatum.STATUS_DB_UNEXECUTED:
                sb.append("unexecuted");
                break;
        }

        sb.append("\nExecuteTime:").append(sdf.format(new Date(datum.getExecuteTime())))
                .append("\nExecuteCount:").append(datum.getExecuteCount());

        int metaIndex = 0;


        for(Entry<String, JsonElement> entry: datum.meta().entrySet()){
            sb.append("\nMETA").append("[").append(metaIndex++).append("]:(")
                    .append(entry.getKey()).append(",").append(entry.getValue()).append(")");
        }


        sb.append("\n");
        return sb.toString();
    }

    public static CrawlDatum jsonStrToDatum(String crawlDatumKey, String jsonStr) {
        JsonArray jsonArray = GsonUtils.parse(jsonStr).getAsJsonArray();

        CrawlDatum crawlDatum = new CrawlDatum();
        crawlDatum.key(crawlDatumKey);
        crawlDatum.url(jsonArray.get(0).getAsString());
        crawlDatum.setStatus(jsonArray.get(1).getAsInt());
        crawlDatum.setExecuteTime(jsonArray.get(2).getAsLong());
        crawlDatum.setExecuteCount(jsonArray.get(3).getAsInt());
        if (jsonArray.size() == 5) {
            JsonObject metaJsonObject = jsonArray.get(4).getAsJsonObject();
            crawlDatum.meta(metaJsonObject);
        }
        return crawlDatum;
    }

    public static String datumToJsonStr(CrawlDatum datum) {

        JsonArray jsonArray = new JsonArray();
        jsonArray.add(datum.url());
        jsonArray.add(datum.getStatus());
        jsonArray.add(datum.getExecuteTime());
        jsonArray.add(datum.getExecuteCount());
        if (datum.meta().size() > 0) {
            jsonArray.add(datum.meta());
        }

        return jsonArray.toString();
    }

}
